var globalLightPopups = new Array("stupdtsec");
var currentOpenPopup = "";
//Supportive function to open lightbox enabled div
function openLightbox(divLight, divFade, divLightSuffix) {
    window.scrollTo(0, 0);
    if (divLightSuffix.toString().length > 0) {
        if (document.getElementById(divLight + "_" + divLightSuffix)) {
            divLight = divLight + "_" + divLightSuffix;
        }
    }
    //Set Global Popup Variable to Currently Opened Popup
    currentOpenPopup = divLight;
    makeCenter(divLight);
    $('#' + divLight).slideDown('slow', function() {
        // Animation complete
        document.getElementById(divLight).style.display = 'block';
        currentOpenPopup = divLight;
    });
    if (document.getElementById(divFade)) {
        document.getElementById(divFade).style.display = 'block';
    }
}


//Function to open Lightbox Popup on center of screen
function lightbox_open(divLight, divFade, divLightSuffix) {
    openLightbox(divLight, divFade, divLightSuffix);
}

//function to close Lightbox Popup
function lightbox_close(divLight, divFade, divLightSuffix) {
    
    globalSignedUrlFor = "";
    if (typeof divLightSuffix == 'undefined') {
        divLightSuffix = "";
    }

    
    //only to ensure if nothing founds then what to do
    var nowActivePopup = getActiveLightDivName();
    if (nowActivePopup != "" && (currentOpenPopup == "" || currentOpenPopup.length == 0)) {
        currentOpenPopup = nowActivePopup;
    }
    if(currentOpenPopup.indexOf("stupdtsec")>0){
        $("#light_stupdtsec").removeClass("statuspopupdiv");
        if (document.getElementById('fade').style.display == 'block') {
            document.getElementById('fade').style.display = 'none';
        }
        return true;    
    }

    if (currentOpenPopup.length > 0) {
        if (document.getElementById(currentOpenPopup)) {
            $('#' + currentOpenPopup).slideUp('slow', function() {
                // Animation complete

                if (currentOpenPopup == "light") {
                    //means some other popup is also open so don't hide fade effect
                    if (document.getElementById(currentOpenPopup) && document.getElementById(currentOpenPopup).style.display == "block") {
                        document.getElementById(currentOpenPopup).style.display = 'none';
                    }

                    if (nowActivePopup.length <= 0) { //means no special popup is opened
                        if (document.getElementById('fade') && document.getElementById('fade').style.display == 'block') {
                            document.getElementById('fade').style.display = 'none';
                        }
                    }
                }
                else {
                    if (document.getElementById(currentOpenPopup) && document.getElementById(currentOpenPopup).style.display == "block") {
                        document.getElementById(currentOpenPopup).style.display = 'none';
                    }

                    if (document.getElementById('fade') && document.getElementById('fade').style.display == 'block') {
                        document.getElementById('fade').style.display = 'none';
                    }
                }
                if (document.getElementById("light_progress") && document.getElementById("light_progress").style.display != 'none') {
                    document.getElementById("light_progress").style.display = 'none';
                }
                currentOpenPopup = nowActivePopup;
            });
        }
    }

    //In any case if popup closed but its faded div is still appearing
    if (currentOpenPopup.length == 0 && document.getElementById('fade')) {
        if (document.getElementById('fade').style.display == 'block') {
            document.getElementById('fade').style.display = 'none';
        }
    }


}

//Function to close current Lightbox Popup
function lightbox_closeCurrent() {

    globalSignedUrlFor = "";

    //only to ensure if nothing founds then what to do
    var nowActivePopup = getActiveLightDivName();
    if (nowActivePopup != "" && (currentOpenPopup == "" || currentOpenPopup.length == 0)) {
        currentOpenPopup = nowActivePopup;
    }

    if(currentOpenPopup.indexOf("stupdtsec")>0){
        $("#light_stupdtsec").removeClass("statuspopupdiv");
        jQuery('#' + currentOpenPopup).css(
            { "left":  "", "top": "", "z-index": "", "position": "" }
        );
        
        if (document.getElementById('fade').style.display == 'block') {
            document.getElementById('fade').style.display = 'none';
        }
        return true;    
    }
    if (currentOpenPopup.length > 0) {
        if (document.getElementById(currentOpenPopup)) {
            $('#' + currentOpenPopup).slideUp('slow', function() {
                // Animation complete
                if (document.getElementById('fade') && document.getElementById('fade').style.display == 'block') {
                    document.getElementById('fade').style.display = 'none';
                }

                if (document.getElementById(currentOpenPopup) && document.getElementById(currentOpenPopup).style.display == "block") {
                    document.getElementById(currentOpenPopup).style.display = 'none';
                }

                if (document.getElementById("light_progress")) {
                    document.getElementById("light_progress").style.display = 'none';
                }
                currentOpenPopup = "";
            });
        }
    }

    //In any case if popup closed but its faded div is still appearing
    if (currentOpenPopup.length == 0 && document.getElementById('fade')) {
        if (document.getElementById('fade').style.display == 'block') {
            document.getElementById('fade').style.display = 'none';
        }
    }
}

//Supportive function to get Active div whose display property is set to 'block' this time
function getActiveFadeDivName() {

    var noneFound = "";
    //traverse for all divs and find which one is having display block and return that divname
    for (var fadedDivCounts = 0; fadedDivCounts < globalLightPopups.length; fadedDivCounts++) {
        if (document.getElementById(globalLightPopups[fadedDivCounts])) {
            if (document.getElementById(globalLightPopups[fadedDivCounts]).style.display == "block") {
                return globalLightPopups[fadedDivCounts];
            }
        }
    }
    return noneFound;
}

function getActiveLightDivName() {

    var noneFound = "";
    //traverse for all divs and find which one is having display block and return that divname
    if (typeof globalLightPopups == 'undefined') {
        noneFound = currentOpenPopup;
    }
    else {
        for (var fadedDivCounts = 0; fadedDivCounts < globalLightPopups.length; fadedDivCounts++) {
            if (document.getElementById("light_" + globalLightPopups[fadedDivCounts])) {
                if (document.getElementById("light_" + globalLightPopups[fadedDivCounts]).style.display == "block") {
                    return "light_" + globalLightPopups[fadedDivCounts];
                }
            }
        }
    }
    return noneFound;
}


//Global code to perform action when window resizes
$(window).resize(function() {
    if (currentOpenPopup!="" && document.getElementById(currentOpenPopup)) {
        makeCenter(currentOpenPopup);
    }
});


//Show popup in center of screen
function makeCenter(divId) {
    // divId is an Id of the div
    var WindowHeight = 0;
    var WindowWidth = 0;

    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined') {
        WindowWidth = window.innerWidth,
		WindowHeight = window.innerHeight
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined'
		   && typeof document.documentElement.clientWidth !=
		   'undefined' && document.documentElement.clientWidth != 0) {
        WindowWidth = document.documentElement.clientWidth,
	       WindowHeight = document.documentElement.clientHeight
    }

    // older versions of IE

    else {
        WindowWidth = document.getElementsByTagName('body')[0].clientWidth,
	       WindowHeight = document.getElementsByTagName('body')[0].clientHeight
    }
    // get the middle height using below formula
    //alert((WindowHeight - jQuery('#' + divId).height()));

    var middleHeight = (WindowHeight - jQuery('#' + divId).height()) / 2;

    // get the middle width using below formula
    var middleWidth = (WindowWidth - jQuery('#' + divId).width()) / 2;


    // Now set the css for provided div Id
    //"left": + middleWidth + "px",

    var ver = getInternetExplorerVersion();
    var zindexforlightpopup = 1003;
    if (divId == "light") {
        zindexforlightpopup = 1005;
    }
    if (ver > -1) {
        // Setup html5 version
        //alert( $.browser.version );
        if (ver > 7) {
            jQuery('#' + divId).css({ "left": +middleWidth + "px", "top": +middleHeight + "px", "z-index": zindexforlightpopup, "position": "fixed" });
        }
        else {


            jQuery('#' + divId).css({ "left": +middleWidth + "px", "top": +middleHeight + "px", "z-index": zindexforlightpopup, "position": "fixed" });
        }
    }
    else {
        jQuery('#' + divId).css({ "left": +middleWidth + "px", "top": +middleHeight + "px", "z-index": zindexforlightpopup, "position": "absolute" });
    }
}


function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

$(document).keydown(function(e) {
    // ESCAPE key pressed
    if (e.keyCode == 27) {
        lightbox_closeCurrent();
    }
});